package com.pingan.cashier.service.impl;

import com.pingan.cashier.dal.mapper.OrderMapper;
import com.pingan.cashier.dal.mapper.PayChannelMapper;
import com.pingan.cashier.service.PayChannelService;
import com.pingan.cashier.service.dto.OrderDTO;
import com.pingan.cashier.service.dto.PayChannelDTO;
import com.pingan.cashier.service.dto.PayMethodDTO;
import com.pingan.cashier.web.req.OrderCreateReq;
import com.pingan.cashier.web.vo.PayChannelVO;
import com.pingan.cashier.web.vo.PayMethodVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class PayChannelServiceImpl implements PayChannelService {

    public static final String STATUS_SUCCESS = "1";
    @Autowired
    private PayChannelMapper payChannelMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<PayChannelVO> queryUserPayChannelList() {

        List<PayChannelVO> payChannelVOList = new ArrayList<>();
        List<PayChannelDTO> payChannelDTOList =  payChannelMapper.querySupportPayChannelList();

        if (null == payChannelDTOList || payChannelDTOList.isEmpty()){
            return payChannelVOList;
        }

        for(PayChannelDTO payChannelDTO : payChannelDTOList){
            PayChannelVO payChannelVO = new PayChannelVO();
            payChannelVO.setId(payChannelDTO.getId());
            payChannelVO.setChannelCode(payChannelDTO.getCode());
            payChannelVO.setChannelName(payChannelDTO.getSimpleName());
            payChannelVO.setLogoUrl(payChannelDTO.getLogo());

            payChannelVOList.add(payChannelVO);

            if (null == payChannelDTO.getPayMethodList()
                    || payChannelDTO.getPayMethodList().isEmpty()){
                continue;
            }
            List<PayMethodVO> payMethodList = new ArrayList<>();
            for(PayMethodDTO payMethodDTO : payChannelDTO.getPayMethodList()){
                PayMethodVO payMethodVO = new PayMethodVO();
                payMethodVO.setPayMethodId(payMethodDTO.getId());
                payMethodVO.setPayMethodCode(payMethodDTO.getCode());
                payMethodVO.setPayMethodName(payMethodDTO.getName());
                payMethodVO.setType(payMethodDTO.getType());
                payMethodVO.setPayMethodLogoUrl(payMethodDTO.getLogo());
                payMethodList.add(payMethodVO);
            }
            payChannelVO.setPayMethodList(payMethodList);
        }

        return payChannelVOList;
    }

    @Override
    public void createOrder(OrderCreateReq orderCreateReq) {
        orderMapper.createOrder(orderCreateReq.getPaymentOrder(),
                orderCreateReq.getChannelCode(), orderCreateReq.getPayMethodCode(),orderCreateReq.getAmount());
    }

    @Override
    public void paymentResultCallback(String paymentOrderNum) {
        orderMapper.updateOrderStatus(paymentOrderNum, STATUS_SUCCESS);
    }


    public void syncPaymentResult(OrderDTO order) {

        // 支付成功，通过消息中间件，通知财务系统

    }

}
